<?php
namespace App\Service\Admin;

class ChatService extends BaseService
{
    //聊天室

    /**
     * 列表
     * @param array $pages【start，limit】
     */
    public function getList($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbChat)->where('del', 0);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            if (substr($v['thumb'], 0, 4) == 'http') {
                $thumb = $v['thumb'];
            } else {
                $thumb = file_exists($v['thumb']) ? env('DOMAIN').'/'.$v['thumb'] : '';
            }
            $user_num = $this->dbConn($this->dbChatUid)
                ->where('del', 0)
                ->where('chat_id', $v['id'])
                ->count();
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'thumb' => $thumb,
                'user_num' => $user_num,
                'is_hot' => $v['is_hot'],
                'hot_name' => $v['is_hot'] ? '是' : '否',
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }
}